package com.xq.tmall.agu.ferign;

import com.xq.tmall.entity.Stock;
import com.xq.tmall.entity.WebPage;
import com.xq.tmall.util.IpFerginUtil;
import com.xq.tmall.util.SpringContextUtil;
import com.xq.tmall.util.TradingDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * http://qt.gtimg.cn/q=%s
 *
 * @author chenguoxiang
 * @create 2019-03-07 10:58
 **/
@Slf4j
public class TencentIndexSpider extends AbstractHtmlSpider<Stock> {

    /**
     * v_sh600293="1~三峡新材~600293~3.08~3.02~3.04~242484~143964~98520~3.07~3125~3.06~3814~3.05~2880~3.04~967~3.03~2973~3.08~6027~3.09~9286~3.10~13906~3.11~7554~3.12~9664~~20250624161405~0.06~1.99~3.08~3.02~3.08/242484/74049071~242484~7405~2.09~-846.13~~3.08~3.02~1.99~35.73~35.73~2.04~3.32~2.72~1.14~-32678~3.05~86.01~284.41~~~1.21~7404.9071~0.0000~0~ ~GP-A~-2.22~0.98~0.00~-0.24~-0.40~4.15~2.32~-0.32~2.67~1.32~1160145046~1160145046~-54.29~27.27~1160145046~~~23.20~0.00~~CNY~0~___D__F__N~3.13~-6215";
     * @param stockCode
     * @param queue
     */
    public TencentIndexSpider(String[] stockCode, BlockingQueue<Stock> queue) {
        StringBuilder queryStr = new StringBuilder();
        IpFerginUtil ipFerginUtil = SpringContextUtil.getBean(IpFerginUtil.class);
        for (String code : stockCode) {
            String resultCode = StockSpider.formatStockCode(code);
            if (null != resultCode) {
                queryStr.append(resultCode).append(",");
            }
        }
        String queryCode = queryStr.toString().replaceAll("\\,$", "");
        String pageUrl = String.format("http://qt.gtimg.cn/q=%s", queryCode);
        this.queue = queue;
        this.pageUrl = pageUrl;
        this.timeOut = 8000;
        this.ignoreContentType = true;
        this.method = Connection.Method.GET;
        this.cookie = ipFerginUtil.getRedisCookies();
        this.useAgent = ipFerginUtil.getUseAgent();
        this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipFerginUtil.getRedisIp(), Integer.parseInt(ipFerginUtil.getRedisPort())));
    }

    @Override
    public void parsePage(WebPage webPage) throws Exception {

            String content = webPage.getDocument().text();
        List<String> rows = Arrays.asList(content.split(";"));
        for (int i = 0; i < rows.size(); i++) {
            String[] item = rows.get(i).replaceAll("v_.*=|\"", "").split("~");
            Stock stock = new Stock();
            stock.setName(item[1]);
            stock.setCode(item[2]);
            stock.setType(StockSpider.formatStockCode(item[2]).replaceAll("\\d", ""));
            stock.setPrice(Double.parseDouble(item[3]));
            stock.setYesterdayPrice(Double.parseDouble(item[4]));
            stock.setTotalValue(Double.parseDouble(item[5]));//今开价格
            stock.setBvps(Double.parseDouble(item[6]));//成交量
            stock.setFluctuate(Double.parseDouble(item[31]));//涨跌额
            stock.setIndustry(item[32]);//涨跌幅
            stock.setPes(Double.parseDouble(item[37]));//成交额

            stock.setPb(Double.parseDouble(item[38]));//换手率
            stock.setTodayMax(Double.parseDouble(item[33]));
            stock.setTodayMin(Double.parseDouble(item[34]));//i最低价
            stock.setAmplitude(Double.parseDouble(item[43]));//振幅
            stock.setPettm((Double.parseDouble(item[44])));//流通市值
            stock.setDividend(Double.parseDouble(item[49]));//量比
            stock.setRoe(Double.parseDouble(item[52])); // 市盈率（动）
            stock.setPriceDate(Long.valueOf(TradingDateUtil.getDateTime()));
            stock.setGoPettm(Double.parseDouble(item[72]));
            queue.offer(stock);

        }
    }
}
